package org.jeecg.modules.paycenter.channel.tonglian;

import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.paycenter.enums.UTypeEnum;
import org.jeecg.modules.wlhy.tlaccount.entity.HyTlAccount;
import org.jeecg.modules.wlhy.tlaccount.entity.HyTlAccountRecord;
import org.jeecg.modules.wlhy.tlaccount.entity.HyTlAccountSysRecord;
import org.jeecg.modules.wlhy.tlaccount.service.IHyTlAccountRecordService;
import org.jeecg.modules.wlhy.tlaccount.service.IHyTlAccountService;
import org.jeecg.modules.wlhy.tlaccount.service.IHyTlAccountSysRecordService;
import org.jeecg.modules.wlhy.utils.CodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Map;
import java.util.TreeMap;

@Service
public class TongLianWalletService {

    @Autowired
    private IHyTlAccountService accountService;

    @Autowired
    private IHyTlAccountRecordService recordService;

    @Autowired
    private IHyTlAccountSysRecordService sysRecordService;

    //检查货主充值是否过期
    @Transactional
    public void CheckCustomerRechargeRecordExpire(String merOrderNo){
        HyTlAccountRecord record = recordService.getByMerOrderNoForUpdate(merOrderNo);
        if (record == null) {
            return;
        }
        if(!"1".equals(record.getOptType())){
            //订单类型错误
            return;
        }
        if(!"0".equals(record.getSt())){
            //已充值成功
            return;
        }
        JSONObject ret = TongLianUtil.ystOrderStatusQuery(merOrderNo);
        if (!TongLianUtil.isSuccess(ret)) {
            return ;
        }
        JSONObject jd = ret.getJSONObject("data");
        String payStatus = jd.getString("payStatus");
        if ("0".equals(payStatus)) {
            record.setSt("4");
            recordService.updateById(record);
            return;
        }
    }

    //充值回调
    @Transactional
    public void RechargeCallback(HyTlAccountRecord record){
        HyTlAccount account = accountService.getByIdForUpdate(record.getWalletId());
        if(account == null){
            return;
        }
        BigDecimal amt = record.getAmt();
        BigDecimal oldAmt = account.getAmt();
        BigDecimal newAmt = oldAmt.add(amt);
        account.setAmt(newAmt);
        record.setLastAmt(newAmt);

        HyTlAccount sysAccount = accountService.getByIdForUpdate("111111");////平台账户
        BigDecimal oldSysAmt = sysAccount.getAmt();
        if(oldSysAmt == null){
            oldSysAmt = BigDecimal.ZERO;
        }
        BigDecimal newSysAmt = oldSysAmt.add(amt);
        sysAccount.setAmt(newSysAmt);
        accountService.updateById(sysAccount);
        accountService.updateById(account);
        recordService.updateById(record);
    }

    @Transactional
    public HyTlAccountRecord exchange(HyTlAccount companyAccount,HyTlAccount customerAccount, BigDecimal amt, BigDecimal costService, BigDecimal costPayShipping ,String remark) {
        if (amt == null) {
            throw new JeecgBootException("金额不正确！");
        }
        //<=0
        if (amt.compareTo(BigDecimal.ZERO) < 1) {
            throw new JeecgBootException("金额不正确！");
        }

        if (customerAccount.getAmt().compareTo(amt) == -1) {
            throw new JeecgBootException("余额不足！");
        }

        //更新分公司账户
        BigDecimal oldComPanyAmt = companyAccount.getAmt();
        if (oldComPanyAmt == null) {
            oldComPanyAmt = BigDecimal.ZERO;
        }
        BigDecimal newComPanyAmt = oldComPanyAmt.add(amt);
        companyAccount.setAmt(newComPanyAmt);
        BigDecimal oldCompanyCostService = companyAccount.getCostService();
        if (oldCompanyCostService == null) {
            oldCompanyCostService = BigDecimal.ZERO;
        }
        BigDecimal newCostService = oldCompanyCostService.add(costService);
        companyAccount.setCostService(newCostService);
        accountService.updateById(companyAccount);

        //更新货主账户
        BigDecimal walletAmt = customerAccount.getAmt();
        if (walletAmt == null) {
            walletAmt = BigDecimal.ZERO;
        }
        BigDecimal newAmt = walletAmt.subtract(amt);
        customerAccount.setAmt(newAmt);
        accountService.updateById(customerAccount);

        //添加货主账户记录
        HyTlAccountRecord record = new HyTlAccountRecord();
        record.setOptType(TlOptTypeEnum.PAY._optType);//操作类型
        record.setRt("2");//支出
        record.setBizUserId(customerAccount.getBizUserId());
        record.setUid(customerAccount.getUid());
        record.setWalletId(customerAccount.getId());
        record.setAmt(amt);
        record.setLastAmt(newAmt);
        record.setCostService(costService);
        record.setCostPayShipping(costPayShipping);
        record.setSt("1");//成功
        record.setRemark(remark);
        record.setMerOrderNo(CodeUtil.getMerOrderNo());
        record.setPayDatetime(DateUtils.getDateStr());
        record.setCompanyId(companyAccount.getUid());
        recordService.save(record);

        //添加分公司账户记录
        HyTlAccountSysRecord sysRecord = new HyTlAccountSysRecord();
        sysRecord.setRecordId(record.getId());
        sysRecord.setOptType(TlOptTypeEnum.SR_FEE._optType);//操作类型
        sysRecord.setUtype(customerAccount.getUtype());
        sysRecord.setRt("1");//收入
        sysRecord.setBizUserId(customerAccount.getBizUserId());
        sysRecord.setUid(customerAccount.getUid());
        sysRecord.setWalletId(customerAccount.getId());
        sysRecord.setAmt(amt);
        sysRecord.setLastAmt(newComPanyAmt);
        sysRecord.setCostService(costService);
        sysRecord.setLastCostService(newCostService);
        sysRecord.setCostPayShipping(costPayShipping);
        sysRecord.setSt("1");//成功
        sysRecord.setRemark("收运费");
        sysRecord.setMerOrderNo(record.getMerOrderNo());
        sysRecord.setPayDatetime(record.getPayDatetime());
        sysRecord.setAcct(customerAccount.getAccNo());
        sysRecord.setAcctName(customerAccount.getAccName());
        sysRecord.setCompanyId(companyAccount.getUid());
        sysRecordService.save(sysRecord);
        return record;
    }

    @Transactional
    public HyTlAccountRecord preExchange(HyTlAccount companyAccount,HyTlAccount customerAccount, BigDecimal amt,String remark) {
        if (amt == null) {
            throw new JeecgBootException("金额不正确！");
        }
        //<=0
        if (amt.compareTo(BigDecimal.ZERO) < 1) {
            throw new JeecgBootException("金额不正确！");
        }

        if (customerAccount.getAmt().compareTo(amt) == -1) {
            throw new JeecgBootException("余额不足！");
        }

        //更新分公司账户
        BigDecimal oldComPanyAmt = companyAccount.getAmt();
        if (oldComPanyAmt == null) {
            oldComPanyAmt = BigDecimal.ZERO;
        }
        BigDecimal newComPanyAmt = oldComPanyAmt.add(amt);
        companyAccount.setAmt(newComPanyAmt);
        accountService.updateById(companyAccount);

        //更新货主账户
        BigDecimal walletAmt = customerAccount.getAmt();
        if (walletAmt == null) {
            walletAmt = BigDecimal.ZERO;
        }
        BigDecimal newAmt = walletAmt.subtract(amt);
        customerAccount.setAmt(newAmt);
        accountService.updateById(customerAccount);

        //添加货主账户记录
        HyTlAccountRecord record = new HyTlAccountRecord();
        record.setOptType(TlOptTypeEnum.PREPAY._optType);//操作类型
        record.setRt("2");//支出
        record.setBizUserId(customerAccount.getBizUserId());
        record.setUid(customerAccount.getUid());
        record.setWalletId(customerAccount.getId());
        record.setAmt(amt);
        record.setLastAmt(newAmt);
        record.setCostService(BigDecimal.ZERO);
        record.setCostPayShipping(amt);
        record.setSt("1");//成功
        record.setRemark(remark);
        record.setMerOrderNo(CodeUtil.getMerOrderNo());
        record.setPayDatetime(DateUtils.getDateStr());
        record.setCompanyId(companyAccount.getUid());
        recordService.save(record);

        //添加分公司账户记录
        HyTlAccountSysRecord sysRecord = new HyTlAccountSysRecord();
        sysRecord.setRecordId(record.getId());
        sysRecord.setOptType(TlOptTypeEnum.SR_PRE_FEE._optType);//操作类型
        sysRecord.setUtype(customerAccount.getUtype());
        sysRecord.setRt("1");//收入
        sysRecord.setBizUserId(customerAccount.getBizUserId());
        sysRecord.setUid(customerAccount.getUid());
        sysRecord.setWalletId(customerAccount.getId());
        sysRecord.setAmt(amt);
        sysRecord.setLastAmt(newComPanyAmt);
//        sysRecord.setCostService(costService);
//        sysRecord.setLastCostService(newCostService);
        sysRecord.setCostPayShipping(amt);
        sysRecord.setSt("1");//成功
        sysRecord.setRemark("收预付运费");
        sysRecord.setMerOrderNo(record.getMerOrderNo());
        sysRecord.setPayDatetime(record.getPayDatetime());
        sysRecord.setAcct(customerAccount.getAccNo());
        sysRecord.setAcctName(customerAccount.getAccName());
        sysRecord.setCompanyId(companyAccount.getUid());
        sysRecordService.save(sysRecord);
        return record;
    }

}
